package com.jiuzhou.guanwang.jzcp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.activity.LastOpenActivity;
import com.jiuzhou.guanwang.jzcp.activity.PastOpenActivity;
import com.jiuzhou.guanwang.jzcp.loader.GlideImageLoader;
import com.jiuzhou.guanwang.jzcp.widget.RiseNumberTextView;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class HomeFragment extends Fragment {

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.tv_red_1)
    RiseNumberTextView tvRed1;
    @BindView(R.id.tv_red_2)
    RiseNumberTextView tvRed2;
    @BindView(R.id.tv_red_3)
    RiseNumberTextView tvRed3;
    @BindView(R.id.tv_red_4)
    RiseNumberTextView tvRed4;
    @BindView(R.id.tv_red_5)
    RiseNumberTextView tvRed5;
    @BindView(R.id.tv_red_6)
    RiseNumberTextView tvRed6;
    @BindView(R.id.tv_blue)
    RiseNumberTextView tvBlue;
    @BindView(R.id.iv_refresh)
    ImageView ivRefresh;
    private View rootView;
    private Unbinder unbinder;
    private ArrayList<Integer> bannerList = new ArrayList<>();
    private ArrayList<Integer> redNums = new ArrayList<>();
    private ArrayList<Integer> blueNums = new ArrayList<>();


    public static HomeFragment newInstance(String info) {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        args.putString("info", info);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            String name = bundle.get("info").toString();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_home, null);
            unbinder = ButterKnife.bind(this, rootView);
            initView();
            initData();
        } else {
            // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，
            // 要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }

        return rootView;
    }

    private void initView() {
        bannerList.add(R.drawable.banner1);
        bannerList.add(R.drawable.banner2);
        bannerList.add(R.drawable.banner3);
        bannerList.add(R.drawable.banner4);
        banner.setImages(bannerList)
                .setImageLoader(new GlideImageLoader())
                .start();

        initHotCp();
        //幸运号码
        refreshNum();
    }

    private void initHotCp() {


    }

    private void initData() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }



    private void refreshNum() {
        randomSSQ();
        for (int i = 0; i < redNums.size(); i++) {
            switch (i) {
                case 0:
                    tvRed1.withNumber(redNums.get(i));
                    tvRed1.setDuration(500);
                    tvRed1.start();
                    break;
                case 1:
                    tvRed2.withNumber(redNums.get(i));
                    tvRed2.setDuration(500);
                    tvRed2.start();
                    break;
                case 2:
                    tvRed3.withNumber(redNums.get(i));
                    tvRed3.setDuration(500);
                    tvRed3.start();
                    break;
                case 3:
                    tvRed4.withNumber(redNums.get(i));
                    tvRed4.setDuration(500);
                    tvRed4.start();
                    break;
                case 4:
                    tvRed5.withNumber(redNums.get(i));
                    tvRed5.setDuration(500);
                    tvRed5.start();
                    break;
                case 5:
                    tvRed6.withNumber(redNums.get(i));
                    tvRed6.setDuration(500);
                    tvRed6.start();
                    break;
            }


        }

        tvBlue.withNumber(blueNums.get(0));
        tvBlue.setDuration(500);
        tvBlue.start();
    }

    //机选一注
    private void randomSSQ() {
        Random random = new Random();
        redNums.clear();
        //机选红球
        while (redNums.size() <= 6) {
            int num = random.nextInt(33) + 1;
            if (redNums.contains(num)) {
                continue;
            }
            redNums.add(num);
        }
        Collections.sort(redNums);

        //机选蓝球
        blueNums.clear();
        int num = random.nextInt(16) + 1;
        blueNums.add(num);
    }

    @OnClick({R.id.rl_ssq, R.id.rl_dlt, R.id.rl_qlc, R.id.rl_3d, R.id.rl_qxc, R.id.rl_pls, R.id.rl_k3, R.id.rl_plw, R.id.rl_js11x5,R.id.tv_blue, R.id.iv_refresh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_ssq:
                jumpto("双色球","ssq");
                break;
            case R.id.rl_dlt:
                jumpto("超级大乐透","dlt");
                break;
            case R.id.rl_qlc:
                jumpto("七乐彩","qlc");
                break;
            case R.id.rl_3d:
                jumpto("福彩3d","fc3d");
                break;
            case R.id.rl_qxc:
                jumpto("七星彩","qxc");
                break;
            case R.id.rl_pls:

                jumpto("排列3","pl3");
                break;
            case R.id.rl_k3:
                Intent intent = new Intent(getActivity(), PastOpenActivity.class);
                intent.putExtra("name", "江苏快三");
                intent.putExtra("code","jsk3");
                startActivity(intent);
                break;
            case R.id.rl_plw:
                jumpto("排列5","pl5");
                break;
            case R.id.rl_js11x5:
                Intent intent2 = new Intent(getActivity(), PastOpenActivity.class);
                intent2.putExtra("name", "江苏11选5");
                intent2.putExtra("code","js11x5");
                startActivity(intent2);
                break;
            case R.id.tv_blue:
                break;
            case R.id.iv_refresh:
                refreshNum();
                break;
        }
    }

    private void jumpto(String name, String code) {
        Intent intent = new Intent(getActivity(), LastOpenActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("code",code);
        startActivity(intent);
    }

}
